package com.linwen.javase.juc;

/**
 * @ClassName _02Thread
 * @Description
 * @Author lin
 * @Date 2022/4/18 下午11:29
 * @Version V1.0
 *
 * 多线程编写步骤
 * 1 创建资源类
 * 2 多线程操作资源类
 */

public class _02SyncThread {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        SalerThread salerThread1 = new SalerThread(ticket);
        SalerThread salerThread2 = new SalerThread(ticket);
        SalerThread salerThread3 = new SalerThread(ticket);

        salerThread1.setName("AAA");
        salerThread1.start();

        salerThread2.setName("BBB");
        salerThread2.start();

        salerThread3.setName("CCC");
        salerThread3.start();
    }
}
class Ticket{
    private  int ticketNum = 30;
    public synchronized void sale(){
        if(ticketNum>0){
            System.out.println(Thread.currentThread().getName()+"正在买票"+ticketNum--);
        }
    }
}
class SalerThread extends Thread{
    private Ticket ticket;
    public SalerThread(Ticket ticket){
        this.ticket = ticket;
    }
    @Override
    public void run() {
        for (int i = 0; i < 300; i++) {
            ticket.sale();
        }
    }
}